﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace FasterWPF
{
    public static class StackPanelExt
    {
        public static StackPanel SetBackgroundColor(this StackPanel chain, Brush brush)
        {
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }
        
        public static StackPanel Initialize(this StackPanel source, ContainerType containerType, Orientation orientation, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, double.NaN, double.NaN, containerType, orientation); //Default width and height to Auto by setting to NaN
        }

        public static StackPanel Initialize(this StackPanel source, double width, double height, ContainerType containerType, Orientation orientation)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Orientation = orientation;
            source.Width = width;
            source.Height = height;

            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<StackPanel>(source);
            source.SetIComposite<StackPanel>(icomp);

            StackPanelParent gbsr = new StackPanelParent();
            source.StoreSetting<StackPanel>("IParent", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<StackPanel>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });

            return source;
        }

        public static TextBox CreateLabelAndTextBox(this StackPanel source, LabelSettings labelSettings, double rowMaxHeight, int maxLength, Thickness questionTextBoxMargin, GridSettings newGridSettings, double questionTextBoxMinWidth, Brush questionTextBoxBackground, int tabIndex)
        {
            var borderWithLabel = FasterWPF.CommonExt.BeginComposite<StackPanel>(source).AddLabel<StackPanel>(0, 0, labelSettings.LabelCaption ?? "", labelSettings.LabelBackground, labelSettings.LabelForeground).EndComposite<StackPanel, StackPanelArgs>(null, newGridSettings);
            var borderWithFNameTxtBox = FasterWPF.CommonExt.BeginComposite<StackPanel>(source).AddAnything<TextBox, StackPanel>(0, 0).EndComposite<StackPanel, StackPanelArgs>(null, newGridSettings);

            var myLabel = borderWithLabel.GetChildFromComposite<Label, StackPanel>(0, 0);
            if (labelSettings.LabelHeight != null)
            {
                myLabel.Height = (double)labelSettings.LabelHeight;
            }
            myLabel.MinWidth = labelSettings.LabelMinWidth;
            myLabel.Margin = labelSettings.LabelMargin;
            myLabel.HorizontalAlignment = HorizontalAlignment.Left;
            FasterWPF.FontExt.ApplyFontSettings(labelSettings.LabelFontSettings, myLabel);

            var myTextBox = borderWithFNameTxtBox.GetChildFromComposite<TextBox, StackPanel>(0, 0);
            myTextBox.TabIndex = tabIndex;
            myTextBox.MaxHeight = rowMaxHeight;
            myTextBox.Background = questionTextBoxBackground;
            myTextBox.MinWidth = questionTextBoxMinWidth;
            myTextBox.Margin = questionTextBoxMargin;
            myTextBox.MaxLength = maxLength;

            return myTextBox;
        }

        public static ComboBox CreateLabelAndComboBox(this StackPanel source, ComboBoxSettings comboBoxSettings, LabelSettings labelSettings, double rowMaxHeight, Thickness comboBoxMargin, GridSettings newGridSettings, Brush comboBoxBackground, int tabIndex,  Action<Object, SelectionChangedEventArgs> optionalSelectionChangedHandler)
        {
            ComboBox comboToAdd = new ComboBox();
            comboToAdd.Initialize(ContainerType.Grid, 1, 1, comboBoxSettings.ScrollViewerHeight, comboBoxSettings.ComboBoxStyle, comboBoxSettings.HighlightForegroundColor, comboBoxSettings.HighlightBackgroundColor);
            comboToAdd.BeginSettings().SetTextMinWidth(0, 0, comboBoxSettings.ComboItemMinWidth).EndSettings();

            ScrollViewerStyle.ApplyToScrollViewer<ComboBox>(comboToAdd, comboBoxSettings.ScrollViewerMouseOverColor, comboBoxSettings.ScrollViewerPressedColor, comboBoxSettings.ScrollViewerBackgroundColor);

            if (comboBoxSettings.ComboItemTextRows == Helpers.ItemTextRows.TwoRowsOfText)
            {
                comboToAdd.SetCompositeGridDimensions<ComboBox>(2, 1, comboBoxSettings.ScrollViewerWidth, GridUnitType.Pixel, 25, GridUnitType.Pixel);
                comboToAdd.BeginSettings<ComboBox>().SetItemBorderSettings<ComboBox>(comboBoxSettings.ComboItemMinWidth, 50, comboBoxSettings.ComboItemBorderColor, comboBoxSettings.ComboItemBorderThickness).EndSettings<ComboBox>();
            }
            else //1 row text
            {
                comboToAdd.SetCompositeGridDimensions<ComboBox>(1, 1, comboBoxSettings.ScrollViewerWidth, GridUnitType.Pixel, 13, GridUnitType.Pixel);
                comboToAdd.BeginSettings<ComboBox>().SetItemBorderSettings<ComboBox>(comboBoxSettings.ComboItemMinWidth, 25, comboBoxSettings.ComboItemBorderColor, comboBoxSettings.ComboItemBorderThickness).EndSettings<ComboBox>();
            }

            if(optionalSelectionChangedHandler!=null)
            {
               comboToAdd.SubscribeEventOnParent<ComboBox, SelectionChangedEventArgs>("SelectionChanged", optionalSelectionChangedHandler);
            }

            var borderWithLabel = FasterWPF.CommonExt.BeginComposite<StackPanel>(source).AddLabel<StackPanel>(0, 0, labelSettings.LabelCaption ?? "", labelSettings.LabelBackground, labelSettings.LabelForeground).EndComposite<StackPanel, StackPanelArgs>(null, newGridSettings);
            var borderWithFNameTxtBox = FasterWPF.CommonExt.BeginComposite<StackPanel>(source).AddExisting<ComboBox, StackPanel>(0, 0, comboToAdd).EndComposite<StackPanel, StackPanelArgs>(null, newGridSettings);

            var myLabel = borderWithLabel.GetChildFromComposite<Label, StackPanel>(0, 0);
            if (labelSettings.LabelHeight != null)
            {
                myLabel.Height = (double)labelSettings.LabelHeight;
            }
            myLabel.MinWidth = labelSettings.LabelMinWidth;
            myLabel.Margin = labelSettings.LabelMargin;
            myLabel.HorizontalAlignment = HorizontalAlignment.Left;
            FasterWPF.FontExt.ApplyFontSettings(labelSettings.LabelFontSettings, myLabel);

            var myComboBox = borderWithFNameTxtBox.GetChildFromComposite<ComboBox, StackPanel>(0, 0);
            myComboBox.TabIndex = tabIndex;
            myComboBox.MaxHeight = rowMaxHeight;
            myComboBox.Background = comboBoxBackground;
            //myComboBox.MinWidth = comboBoxSettings.ComboMinWidth;
            myComboBox.Margin = comboBoxMargin;
            //myTextBox.MaxLength = maxLength;

            return myComboBox;
        }

        public static ListBox CreateLabelAndListBox(this StackPanel source, ListBoxSettings listBoxSettings, LabelSettings labelSettings, double rowMaxHeight, Thickness comboBoxMargin, GridSettings newGridSettingsLabel, GridSettings newGridSettingsListBox, Brush comboBoxBackground, int tabIndex, Action<Object, SelectionChangedEventArgs> optionalSelectionChangedHandler)
        {
            ListBox listboxToAdd = new ListBox();
            listboxToAdd.Initialize(listBoxSettings.Width, listBoxSettings.Height, ContainerType.Grid);

            //listBoxSettings.HighlightForegroundColor, listBoxSettings.HighlightBackgroundColor);

            listboxToAdd.BeginSettings().SetTextMinWidth(0, 0, listBoxSettings.ItemMinWidth).EndSettings();

            ScrollViewerStyle.ApplyToScrollViewer<ListBox>(listboxToAdd, listBoxSettings.ScrollViewerMouseOverColor, listBoxSettings.ScrollViewerPressedColor, listBoxSettings.ScrollViewerBackgroundColor);

            if (listBoxSettings.ItemTextRows == Helpers.ItemTextRows.TwoRowsOfText)
            {
                listboxToAdd.SetCompositeGridDimensions<ListBox>(2, 1, listBoxSettings.Width, GridUnitType.Pixel, listBoxSettings.ItemHeight, GridUnitType.Pixel);
                listboxToAdd.BeginSettings<ListBox>().SetItemBorderSettings<ListBox>(listBoxSettings.ItemMinWidth, listBoxSettings.ItemHeight * 2, listBoxSettings.ItemBorderColor, listBoxSettings.ItemBorderThickness).EndSettings<ListBox>();
            }
            else //1 row text
            {
                listboxToAdd.SetCompositeGridDimensions<ListBox>(1, 1, listBoxSettings.Width, GridUnitType.Pixel, listBoxSettings.ItemHeight, GridUnitType.Pixel);
                listboxToAdd.BeginSettings<ListBox>().SetItemBorderSettings<ListBox>(listBoxSettings.ItemMinWidth, listBoxSettings.ItemHeight * 2, listBoxSettings.ItemBorderColor, listBoxSettings.ItemBorderThickness).EndSettings<ListBox>();
            }

            if (optionalSelectionChangedHandler != null)
            {
                listboxToAdd.SubscribeEventOnParent<ListBox, SelectionChangedEventArgs>("SelectionChanged", optionalSelectionChangedHandler);
            }

            var borderWithLabel = FasterWPF.CommonExt.BeginComposite<StackPanel>(source)
                .AddLabel<StackPanel>(0, 0, labelSettings.LabelCaption ?? "", labelSettings.LabelBackground, labelSettings.LabelForeground)
                .EndComposite<StackPanel, StackPanelArgs>(null, newGridSettingsLabel);

            var borderWithFNameTxtBox = FasterWPF.CommonExt.BeginComposite<StackPanel>(source)
                .AddExisting<ListBox, StackPanel>(0, 0, listboxToAdd)
                .EndComposite<StackPanel, StackPanelArgs>(null, newGridSettingsListBox);

            var myLabel = borderWithLabel.GetChildFromComposite<Label, StackPanel>(0, 0);
            if (labelSettings.LabelHeight != null)
            {
                myLabel.Height = (double)labelSettings.LabelHeight;
            }
            myLabel.MinWidth = labelSettings.LabelMinWidth;
            myLabel.Margin = labelSettings.LabelMargin;
            myLabel.HorizontalAlignment = HorizontalAlignment.Left;
            FasterWPF.FontExt.ApplyFontSettings(labelSettings.LabelFontSettings, myLabel);

            var myListBox = borderWithFNameTxtBox.GetChildFromComposite<ListBox, StackPanel>(0, 0);
            myListBox.TabIndex = tabIndex;
           // myListBox.MaxHeight = rowMaxHeight;
            myListBox.Background = comboBoxBackground;
            //myComboBox.MinWidth = comboBoxSettings.ComboMinWidth;
            myListBox.Margin = comboBoxMargin;
            //myTextBox.MaxLength = maxLength;

            return myListBox;
        }
        
    }//end of class
}//end of namespace
